//
//  KeyboardEventDispatchCore.m
//  HHNMobileCore
//
//  Created by huhmf on 15/7/28.
//  Copyright (c) 2015年 huhmf. All rights reserved.
//

#import "KeyboardEventDispatchCore.h"
#import <UIKit/UIKit.h>
#import "KeyBoardEventClient.h"

@implementation KeyboardEventDispatchCore

+ (void)load
{
    GetCore(KeyboardEventDispatchCore);
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)onKeyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    
    NSValue *frameBegin = userInfo[UIKeyboardFrameBeginUserInfoKey];
    NSValue *frameEnd = userInfo[UIKeyboardFrameEndUserInfoKey];
    NSNumber *animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *animationCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey];
    
    NotifyCoreClient(KeyBoardEventClient, @selector(keyboardWillShowWithFrameBegin:frameEnd:animationDuration:animationCurve:), keyboardWillShowWithFrameBegin:frameBegin frameEnd:frameEnd animationDuration:animationDuration animationCurve:animationCurve);
}

- (void)onKeyboardDidShow:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    
    NSValue *frameBegin = userInfo[UIKeyboardFrameBeginUserInfoKey];
    NSValue *frameEnd = userInfo[UIKeyboardFrameEndUserInfoKey];
    NSNumber *animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *animationCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey];
    
    NotifyCoreClient(KeyBoardEventClient, @selector(keyboardDidShowWithFrameBegin:frameEnd:animationDuration:animationCurve:), keyboardDidShowWithFrameBegin:frameBegin frameEnd:frameEnd animationDuration:animationDuration animationCurve:[animationCurve intValue]);
}

- (void)onKeyboardWillHide:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    
    NSValue *frameBegin = userInfo[UIKeyboardFrameBeginUserInfoKey];
    NSValue *frameEnd = userInfo[UIKeyboardFrameEndUserInfoKey];
    NSNumber *animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *animationCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey];
    
    NotifyCoreClient(KeyBoardEventClient, @selector(keyboardWillHideWithFrameBegin:frameEnd:animationDuration:animationCurve:), keyboardWillHideWithFrameBegin:frameBegin frameEnd:frameEnd animationDuration:animationDuration animationCurve:animationCurve);
}

- (void)onKeyboardDidHide:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    
    NSValue *frameBegin = userInfo[UIKeyboardFrameBeginUserInfoKey];
    NSValue *frameEnd = userInfo[UIKeyboardFrameEndUserInfoKey];
    NSNumber *animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *animationCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey];
    
    NotifyCoreClient(KeyBoardEventClient, @selector(keyboardDidHideWithFrameBegin:frameEnd:animationDuration:animationCurve:), keyboardDidHideWithFrameBegin:frameBegin frameEnd:frameEnd animationDuration:animationDuration animationCurve:animationCurve);
}

@end